var $onDisplayDropDown = null;
var $onDisplayLink = null;
	
jQuery(function(){
	jQuery.ajaxSetup({
		cache: false
	});
	
	jQuery(".nofiMenu").each(function(){
		var $this = jQuery(this);
		var $link = $this.children("a.menuPosition");
		var regconizeClass = $link.attr("rel");
		var $dropDownBox = $this.children("div.dropdownBox[rel=data]");
		var $dropDownBoxNoData = $this.children("div.dropdownBox[rel=nodata]");
		var $numberOfMessage = $this.children("span.numberOfMessage");
		var $listUserInBox = $this.find("ul.listUserInBox");
		var $loading = $this.find("div.loading");
		var $nextPage = $this.find("a.next_page");
		$link.click(function(){					
			if($dropDownBox.css("display") == "none" && $dropDownBoxNoData.css("display") == "none"){
				hideOnDisplayMessageBox();
				
				if(regconizeClass=='userNotification'){
					$listUserInBox.hide();
					$loading.fadeIn();
					
					var unreadList = new Array(); 
					var count = $numberOfMessage.html();
					count = parseInt(count);
					if(count > 0){
						UserManagerExt.markReadNotification(unreadList);
					}
//					$onDisplayDropDown = $dropDownBox;
					$onDisplayLink = $link;
					loadUserNotification('-1', $loading, $listUserInBox, $dropDownBox, $link, $nextPage, $dropDownBoxNoData);
				}else if(regconizeClass=='messageTop'){
					$dropDownBox.show();
					$onDisplayDropDown = $dropDownBox;
					$onDisplayLink = $link;
				}else if(regconizeClass=='requestFriend'){
//					$dropDownBox.show();
//					$onDisplayDropDown = $dropDownBox;
					$onDisplayLink = $link;
					loadFriendRequest($dropDownBox, $listUserInBox, $dropDownBoxNoData);
				}				
				$link.removeClass(regconizeClass).addClass(regconizeClass+"Clicked");
				hideNumberOfMessage($numberOfMessage);				
			}else{
				$link.removeClass(regconizeClass+"Clicked").addClass(regconizeClass);
				$dropDownBox.hide();
				$dropDownBoxNoData.hide();
				$onDisplayDropDown = null;
				$onDisplayLink = null;
			}
			return false;
		});
		
		$nextPage.click(function(){
			if(regconizeClass=='userNotification'){
				$listUserInBox.slideUp();
				$loading.fadeIn();
				var updateDate = $listUserInBox.find("li:last").attr("id");
				if(updateDate == undefined)
					udpateDate = '-1';
				loadUserNotification(updateDate, $loading, $listUserInBox, $dropDownBox, $link, $nextPage);
			}
			return false;
		});
	});
	
	function hideNumberOfMessage($message){
		$message.css("visibility","hidden");
	}
	
	function showNumberOfMessage($message){
		$message.css("visibility","visible");
	}
	
	function hideOnDisplayMessageBox(){
		if($onDisplayDropDown != null && $onDisplayLink != null){
			$onDisplayDropDown.hide();
			var regconizeClass = $onDisplayLink.attr("rel");
			$onDisplayLink.removeClass(regconizeClass+"Clicked").addClass(regconizeClass);
		}
	}
	
	function loadInformation(type){
		switch(type){
			case "userNotification": break;
			case "requestFriend": break;
			case "messageTop": break;
		}
	}
	
	function autoRefreshLog(){
		UserManagerExt.getNewNotification(function(result){
			if(result > 0){
				var $message = jQuery("#userNotification").children("span.numberOfMessage");
				$message.html(result);
				showNumberOfMessage($message);
			}
			setTimeout(autoRefreshLog, 10000);
		});
	}
	
	autoRefreshLog();
});

function loadUserNotification(updateDate, $loading, $listUserInBox, $dropDownBox, $link, $nextPage, $dropDownBoxNoData){
	var _url = ctx+'/ajax/secure/userNotification.html?lastOrderValue='+updateDate;
	jQuery.ajax({
		url: _url,
		success: function(result){		
			if(jQuery.trim(result) != "none"){
				$loading.hide();
				if($dropDownBoxNoData){
					$dropDownBoxNoData.hide();
				}
				$onDisplayDropDown = $dropDownBox;
				$dropDownBox.show();
				$nextPage.show();
				$listUserInBox.html("").append(result);
				$listUserInBox.slideDown();
			}else{
				$loading.hide();
				$nextPage.hide();
				if(updateDate == '-1' && $dropDownBoxNoData && $listUserInBox.children("li").length == 0){
					$dropDownBox.hide();
					$dropDownBoxNoData.show();
					$onDisplayDropDown = $dropDownBoxNoData;
				}
			}						
		}
	});	
}

function loadFriendRequest($dropDownBox, $listUserInBox, $dropDownBoxNoData){
	if($listUserInBox.children("li").length == 0){
		$onDisplayDropDown = $dropDownBoxNoData;
		$dropDownBoxNoData.show();
	}else{
		$onDisplayDropDown = $dropDownBox;
		$dropDownBox.show();
	}
}

function follow(username, id) {
	UserManagerExt.addFriend(id, function(result) {
		if (result != -1) {
			jQuery("div.FollowFriend").children("a").addClass('FollowWait');
		}
	});
}

function acceptFriendRequest(from){
	UserManagerExt.acceptFriend(from, function(result){
		if(result != -1)
			removeFriendRequest(from);
		else
			iAlert("Không thể cập nhật yêu cầu kết bạn");			
	});
}

function removeFriendRequest(from){
	jQuery("ul.listUserInBox li[rel="+from+"]").fadeOut().remove();
	var count = jQuery("ul.listUserInBox li").length;
	if(count == 0){
		jQuery("ul.listUserInBox").prev("p").remove();
	}
}

function refuseFriendRequest(from){
	UserManagerExt.refuseFriend(from, function(result){
		if(result != -1)
			removeFriendRequest(from);
		else
			iAlert("Không thể hủy yêu cầu kết bạn");
	});
}

function stopFollow(username){
	UserManagerExt.refuseFriend(username, function(result){
		if(result != -1)
			jQuery("div.FollowFriend").fadeOut();
			//jQuery("div.FollowFriend a").removeClass("Followed").attr("href","javascript:void(0)");
		else
			iAlert("Không thể hủy yêu cầu kết bạn");
	});
}